﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.Button = function(element) {
    YUIAspNet.Button.initializeBase(this, [element]);
}

YUIAspNet.Button.prototype = {
    initialize: function() {
        YUIAspNet.Button.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function(sender, args) {
        var onClickHandler = null;
        var buttonType = this.get_ButtonType();
        var sender = this;
        var actualHandler = null;

        var buttonType = this.get_ButtonType();

        if (buttonType == "push")
            actualHandler = function() { sender.DoPostback(''); }

        if (this._events) {
            var handler = this._events.getHandler("onClick");

            var args = { cancel: false, eventName: "onClick" };

            actualHandler =
                function() {
                    var result = handler(sender, args);

                    if (args.cancel) return false;
                    if (buttonType == "push")
                        sender.DoPostback('');
                };
        }

        this._childItem = null;

        if (this.get_MenuContainer()) {
            if (this.get_IsOverlay()) {
                this._childItem = new YAHOO.widget.Overlay(this.get_MenuContainer(), { visible: false });

                var olID = this.get_MenuContainer();
            }
            else {
                var currentMenu = $find(this.get_MenuContainer());
                currentMenu.initializeYUIControl();
                this._childItem = currentMenu.get_YUIControl();
                currentMenu.set_Parent(this);
            }
        }

        // no menu items provided. 
        // fallback to regular button
        if ((buttonType == "split" || buttonType == "menu") && !this._childItem)
            buttonType = "push";

        var button = null;

        button = new YAHOO.widget.Button
            ({
                type: buttonType,
                label: this.get_Text(),
                id: this.get_ButtonID(),
                name: this.get_ButtonName(),
                value: this.get_Text(),
                tabindex: this.get_TabIndex(),
                container: this.get_element(),
                href: this.get_NavigateUrl(),
                menu: this._childItem,
                target: this.get_Target(),
                lazyloadmenu: false,
                checked: this.get_Checked(),
                value: this.get_Value()
            });

        if (actualHandler && buttonType != "link" && buttonType != "menu")
            button.on("click", actualHandler);
            
        if(buttonType == "checkbox") {
            var stateID = this.get_StateFieldID();
            button.on("checkedChange", function(state) {
                $get(stateID).value = state.newValue ? "1" : "0";
                });
        }

        this._yuiControl = button;
    },

    get_Overlay: function() {
        return this._childItem;
    },

    dispose: function() {
        $clearHandlers(this.get_element());
        YUIAspNet.Button.callBaseMethod(this, 'dispose');
    },

    get_IsOverlay: function() {
        return this._isOverlay;
    },
    set_IsOverlay: function(isOverlay) {
        this._isOverlay = isOverlay;
    },

    get_ButtonID: function() {
        return this._buttonID;
    },
    set_ButtonID: function(buttonID) {
        this._buttonID = buttonID;
    },

    get_MenuContainer: function() {
        return this._menuContainer;
    },
    set_MenuContainer: function(menuContainer) {
        this._menuContainer = menuContainer;
    },

    get_ButtonName: function() {
        return this._buttonName;
    },
    set_ButtonName: function(buttonName) {
        this._buttonName = buttonName;
    },

    get_TabIndex: function() {
        return this._tabIndex;
    },
    set_TabIndex: function(tabIndex) {
        this._tabIndex = tabIndex;
    },

    get_ButtonType: function() {
        return this._buttonType;
    },
    set_ButtonType: function(buttonType) {
        this._buttonType = buttonType;
    },

    get_Text: function() {
        return this._text;
    },
    set_Text: function(text) {
        this._text = text;
    },

    get_NavigateUrl: function() {
        return this._navigateUrl;
    },
    set_NavigateUrl: function(navigateUrl) {
        this._navigateUrl = navigateUrl;
    },

    get_Target: function() {
        return this._target;
    },
    set_Target: function(target) {
        this._target = target;
    },

    get_Items: function() {
        return this._items;
    },
    set_Items: function(items) {
        this._items = items;
    },

    get_Value: function() {
        return this._value;
    },
    set_Value: function(value) {
        this._value = value;
    },

    get_Checked: function() {
        return this._checked;
    },
    set_Checked: function(checked) {
        this._checked = checked;
    },

    get_RadioButtonItems: function() {
        return this._radioButtonItems;
    },
    set_RadioButtonItems: function(radioButtonItems) {
        this._radioButtonItems = radioButtonItems;
    },

    add_onClick: function(handler) {
        this.get_events().addHandler("onClick", handler);
    },

    remove_onClick: function(handler) {
        this.get_events().removeHandler("onClick", handler);
    }
}

YUIAspNet.Button.registerClass('YUIAspNet.Button', YUIAspNet.BaseControl);
